﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
using System.Collections;
using System.Web.UI.DataVisualization.Charting;
namespace Web
{
    public partial class SchoolChartCognitive : System.Web.UI.Page
    {
        protected int examroomid = 0, schoolid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "StatistScore"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (!PageValidate.QueryStringIsEmpty("schoolid"))
            {
                try
                {
                    schoolid = int.Parse(PageValidate.QueryString("schoolid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!PageValidate.QueryStringIsEmpty("examroomid"))
            {
                try
                {
                    examroomid = int.Parse(PageValidate.QueryString("examroomid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!IsPostBack)
            {
                if (examroomid > 0 && schoolid > 0)
                {
                    IList<int> gradeIds = Grade.GetGradeIdBySchoolId(schoolid);
                    IList<TClassInfo> classInfos = TClass.GetClassInfoByGradeId(gradeIds);

                    IList<StudentScoreExtendInfo> studentscoreinfos = new List<StudentScoreExtendInfo>();
                    studentscoreinfos = StudentScoreExtend.GetList(examroomid, 0, schoolid, 0, 0, 0, 0, 0);
                    ExamRoomInfo examroomInfo = ExamRoom.GetSingleInfo(examroomid);
                    if (examroomInfo == null)
                        return;
                    IList<CognitiveLevelInfo> listCognitiveLevel = GetCognitiveLeve();
                    IList<QuestionInfo> listQuestion = GetQuestion(examroomInfo.ExamPaperId);

                    StringBuilder headerStr = new StringBuilder();
                    headerStr.Append("<tr>");
                    headerStr.Append("<th width='15%'>认识能力</th>");
                    IList<int> classIds = new List<int>();
                    foreach (TClassInfo classInfo in classInfos)
                    {
                        IList<StudentScoreExtendInfo> classscoreInfos = studentscoreinfos.Where(t => t.ClassId == classInfo.Id).ToList();
                        if (classscoreInfos.Count == 0)
                        {
                            continue;
                        }
                        classIds.Add(classInfo.Id);
                        headerStr.Append("<th width='10%'>");
                        headerStr.Append(classInfo.Name);
                        headerStr.Append("</th>");
                        Chart1.Series.Add(new Series()
                        {
                            ChartType = SeriesChartType.Spline,
                            Name = "Class" + classInfo.Id.ToString(),
                            LegendText = classInfo.Name,
                            XValueType = ChartValueType.Double,
                            BorderWidth = 2,
                            ChartArea = "ChartArea1"
                        });
                    }
                    headerStr.Append("</tr>");
                    this.HeaderList.InnerHtml = headerStr.ToString();

                    double v = 0.5, s = 1.5;
                    StringBuilder bodyStr = new StringBuilder();
                    foreach (CognitiveLevelInfo cognitive in listCognitiveLevel)
                    {
                        decimal questScoreCount = 0;
                        IList<int> questIds = new List<int>();
                        foreach (QuestionInfo quest in listQuestion)
                        {
                            if (quest.CognitiveLevelId == cognitive.Id)
                            {
                                questScoreCount += quest.Score;
                                questIds.Add(quest.Id);

                            }
                        }
                        if (questIds.Count == 0)
                        {
                            continue;
                        }
                        Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
                        {
                            Text = cognitive.Name,
                            FromPosition = v,
                            ToPosition = s
                        });
                        v = v + 1;
                        s = s + 1;
                        bodyStr.Append("<tr>");
                        bodyStr.Append("<td class='text_left'>");
                        bodyStr.Append(cognitive.Name);
                        bodyStr.Append("</td>");
                        for (int i = 0; i < classIds.Count; i++)
                        {
                            decimal result = GetClassScore(studentscoreinfos, classIds[i], questIds, questScoreCount);
                            bodyStr.Append("<td>");
                            bodyStr.Append(result + "%");
                            bodyStr.Append("</td>");
                            Chart1.Series["Class" + classIds[i].ToString()].Points.AddY(result);
                        }
                        bodyStr.Append("</tr>");
                    }
                    this.DataList.InnerHtml = bodyStr.ToString();
                }
                else
                {
                    Response.Redirect("AdminExamRoom.aspx");
                }
            }
        }

        private decimal GetClassScore(IList<StudentScoreExtendInfo> studentscoreinfos, int classId, IList<int> questIds, decimal count)
        {
            decimal percent = 0;
            decimal score = 0;
            IList<StudentScoreExtendInfo> schoolscoreInfos = studentscoreinfos.Where(t => t.ClassId == classId).ToList();
            foreach (StudentScoreExtendInfo info in schoolscoreInfos)
            {
                IList<ScoreInfo> scoreInfos = ScoreHelper.GetScoreList(info.QuestItemScore);
                scoreInfos = scoreInfos.Where(t => GetArray<int>.Get(questIds).Contains(t.Id)).ToList();
                foreach (ScoreInfo scoreInfo in scoreInfos)
                {
                    if (!PageValidate.IsEmpty(scoreInfo.Score))
                    {
                        score += decimal.Parse(scoreInfo.Score);
                    }
                }
            }
            if (score > 0)
            {
                percent = Math.Round(score / (count * schoolscoreInfos.Count) * 100, 1);
            }
            return percent;
        }

        private IList<CognitiveLevelInfo> GetCognitiveLeve()
        {
            IList<CognitiveLevelInfo> infos = new List<CognitiveLevelInfo>();
            infos = CognitiveLevel.GetList(0);
            return infos;
        }
        private IList<QuestionInfo> GetQuestion(int exampaperid)
        {
            IList<QuestionInfo> infos = new List<QuestionInfo>();
            infos = Question.GetQuestions(exampaperid, "", "", "", "", "", "", "", "", "", 0, 0);
            return infos;
        }
    }
}
